package com.kk.Utils;

import com.kk.work06.entity.Sales;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class SalesFileUtil {
    public static List<Sales> processFile(MultipartFile file) {
        List<Sales> salesList = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
            // 跳过表头
            reader.readLine();
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length == 3) {
                    Sales sales = new Sales();
                    try {
                        sales.setId(Integer.parseInt(parts[0].trim()));
                        sales.setMouth(Integer.parseInt(parts[1].trim()));
                        sales.setAmount(Double.parseDouble(parts[2].trim()));
                        salesList.add(sales);
                    } catch (NumberFormatException e) {
                        // 处理数据转换异常
                        System.err.println("数据转换错误: " + line);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return salesList;
    }
}